//
//  WebViewController.m
//  DaJiang
//
//  Created by sally on 16/11/30.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation WebViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  
  //url encode
  NSMutableURLRequest *request;
  [self.webView setDelegate:self];
  [[CommonMethod sharedCommonMethod]createProgressHUDInView:self.view withMessage:@""];
  NSString *encodedUrl = [self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl]];
  [self.webView loadRequest:request];

}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self setNav];
  [self setWebCookies];
}

- (void)initControl {
  self.title = @"跳转页面";
  
  // 去掉webview 上下拖动的阴影
  for (UIView *subView in [self.webView subviews]) {
    if ([subView isKindOfClass:[UIScrollView class]]) {
      for (UIView *shadowView in [subView subviews]) {
        if ([shadowView isKindOfClass:[UIImageView class]]) {
          shadowView.hidden = YES;
        }
      }
    }
  }
}

#pragma mark webview delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
  [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  self.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
  
  // 禁用用户选择
  [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
  
  // 禁用长按弹出框
  [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  NSString *urlString = [[request URL] absoluteString];
  NSLog(@"urlString:%@", urlString);
  
  if ([@"ios" isEqualToString:request.URL.scheme]) {
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@":"];
    NSString *method = [request.URL.absoluteString substringFromIndex:range.location + 1];
    SEL selector = NSSelectorFromString(method);
    if ([self respondsToSelector:selector]) {
      [self performSelector:selector];
    }
    return NO;
  }
  
  return YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
  [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"加载出错"];
}

#pragma mark - 私有方法
- (void)setNav {
  UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  CGFloat returnW = 26;
  CGFloat returnH = 21;
  backButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  backButton.frame = CGRectMake(0, 0, returnW, returnH);
  [backButton setImage:[UIImage imageNamed:@"ic_back"] forState:UIControlStateNormal];
  [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
  UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
  backButtonItem.style = UIBarButtonItemStylePlain;
  
  UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
  
  NSArray *buttonArray = [[NSArray alloc]initWithObjects:backButtonItem,saveButton, nil];
  self.navigationItem.leftBarButtonItems = buttonArray;
}

- (void)setWebCookies {
}

- (void)goBack {
  if (self.webView.canGoBack) {
    [self.webView goBack];
  } else {
    [self.navigationController popViewControllerAnimated:YES];
  }
}

- (void)back {
  [self.navigationController popViewControllerAnimated:YES];
}



@end
